package bev.frames.client;

import java.util.EnumSet;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import bev.ConnectionManager;
import bev.article.ArticleType;
import bev.article.BaseArticle;
import bev.article.PersonArticle;
import bev.article.PlaceArticle;
import bev.article.EventArticle;
import bev.article.ObjectArticle;

public class ToolBar extends JToolBar {
    private static final long serialVersionUID = 727945727365564982L;

    ClientFrame parentFrame;

    // Search filter buttons.
    JToggleButton btnPeopleFilter;
    JToggleButton btnPlacesFilter;
    JToggleButton btnEventsFilter;
    JToggleButton btnObjectsFilter;

    // Other buttons.
    JButton btnNewArticle;

    public ToolBar( ClientFrame parentFrame ) {
        this.parentFrame = parentFrame;
        setBorder( new EmptyBorder( 5, 10, 5, 10 ) );

        // New Article button.
        btnNewArticle = new JButton();
        btnNewArticle.setText( "New Article" );
        btnNewArticle.addActionListener( new NewArticleButtonAction() );
        add( btnNewArticle );

        // Right-align filter buttons.
        add( Box.createHorizontalGlue() );

        // People button.
        btnPeopleFilter = new JToggleButton();
        btnPeopleFilter.setText( "People" );
        btnPeopleFilter.addActionListener( new FilterButtonToggled() );
        add( btnPeopleFilter );

        // Places button.
        btnPlacesFilter = new JToggleButton();
        btnPlacesFilter.setText( "Places" );
        btnPlacesFilter.addActionListener( new FilterButtonToggled() );
        add( btnPlacesFilter );

        // Events button.
        btnEventsFilter = new JToggleButton();
        btnEventsFilter.setText( "Events" );
        btnEventsFilter.addActionListener( new FilterButtonToggled() );
        add( btnEventsFilter );

        // Objects button.
        btnObjectsFilter = new JToggleButton();
        btnObjectsFilter.setText( "Objects" );
        btnObjectsFilter.addActionListener( new FilterButtonToggled() );
        add( btnObjectsFilter );
    }

    // Returns an EnumSet object of ArticleType enums set up to reflect the
    // current configuration
    // of the article filter buttons (toggleable for people, places, etc for
    // search filtering).
    public EnumSet<ArticleType> getArticleFilterTypes() {
        EnumSet<ArticleType> articleTypes = EnumSet.noneOf( ArticleType.class );
        if ( btnPeopleFilter.isSelected() )
            articleTypes.add( ArticleType.PERSON );
        if ( btnPlacesFilter.isSelected() )
            articleTypes.add( ArticleType.PLACE );
        if ( btnEventsFilter.isSelected() )
            articleTypes.add( ArticleType.EVENT );
        if ( btnObjectsFilter.isSelected() )
            articleTypes.add( ArticleType.OBJECT );

        // If no buttons were selected (resulting in an empty EnumSet),
        // don't filter on anything. Show all article types.
        if ( articleTypes.size() == 0 )
            articleTypes = EnumSet.allOf( ArticleType.class );

        return articleTypes;
    }

    // Runs an updated filtered query when the search filter buttons are
    // toggled.
    class FilterButtonToggled implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            ToolBar.this.parentFrame.filterArticleView();
        }
    }

    // Handles clicking on the new article button.
    class NewArticleButtonAction implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if ( !ConnectionManager.isConnected() ) {
                JOptionPane
                        .showMessageDialog(
                                ToolBar.this.parentFrame,
                                "Not connected to a project database! You must be connected\nto a project database before you can create a new article." );
                return;
            }

            final JDialog newArtDiag = new JDialog( ToolBar.this.parentFrame,
                    "New Article", true );
            newArtDiag.setSize( 300, 185 );
            newArtDiag.setResizable( false );
            newArtDiag.setLocationRelativeTo( ToolBar.this.parentFrame );
            newArtDiag.setLayout( new GridBagLayout() );

            // List label.
            GridBagConstraints gbcLbl = new GridBagConstraints();
            gbcLbl.anchor = GridBagConstraints.LINE_START;
            gbcLbl.insets = new Insets( 5, 10, 5, 10 );
            gbcLbl.gridx = 0;
            gbcLbl.gridy = 0;
            JLabel lblList = new JLabel( "Article Type" );
            newArtDiag.getContentPane().add( lblList, gbcLbl );

            // Article type list.
            String[] artOptions = { "Person", "Place", "Event", "Object" };

            GridBagConstraints gbcList = new GridBagConstraints();
            gbcList.fill = GridBagConstraints.BOTH;
            gbcList.insets = new Insets( 0, 10, 5, 10 );
            gbcList.gridx = 0;
            gbcList.gridy = 1;
            gbcList.weightx = 1.0;
            gbcList.weighty = 1.0;

            final JList<String> lstArtOptions = new JList<String>( artOptions );
            lstArtOptions
                    .setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
            lstArtOptions.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
            lstArtOptions.setSelectedIndex( 0 );
            JScrollPane listScrlPane = new JScrollPane( lstArtOptions );
            newArtDiag.getContentPane().add( listScrlPane, gbcList );

            // 'Ok' and 'Cancel' buttons.
            JButton btnCancel = new JButton( "Cancel" );
            btnCancel.addActionListener( new ActionListener() {
                public void actionPerformed( ActionEvent e ) {
                    newArtDiag.dispose();
                }
            } );

            JButton btnOk = new JButton( "Ok" );
            btnOk.addActionListener( new ActionListener() {
                public void actionPerformed( ActionEvent e ) {
                    String value = (String) lstArtOptions.getSelectedValue();
                    BaseArticle article;
                    switch ( value.toLowerCase() ) {
                        case "person":
                            article = new PersonArticle();
                            break;
                        case "place":
                            article = new PlaceArticle();
                            break;
                        case "event":
                            article = new EventArticle();
                            break;
                        case "object":
                            article = new ObjectArticle();
                            break;
                        default:
                            System.err.printf(
                                    "ERROR: Article type not supported: %s\n",
                                    value );
                            newArtDiag.dispose();
                            return;
                    }

                    ToolBar.this.parentFrame.setCurrentArticle( article );
                    newArtDiag.dispose();
                }
            } );

            JPanel ctrlBtnPanel = new JPanel();
            ctrlBtnPanel.add( btnCancel );
            ctrlBtnPanel.add( btnOk );

            GridBagConstraints gbcCtrlBtns = new GridBagConstraints();
            gbcCtrlBtns.anchor = GridBagConstraints.LINE_END;
            gbcCtrlBtns.insets = new Insets( 0, 10, 5, 10 );
            gbcCtrlBtns.gridx = 0;
            gbcCtrlBtns.gridy = 2;
            newArtDiag.getContentPane().add( ctrlBtnPanel, gbcCtrlBtns );

            newArtDiag.setVisible( true );
        }
    }
}
